﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Controller;
using Interface;

namespace IAI
{
    public partial class UCOperator : UserControl //Redigeret af Tien, Trang, Mads Enemark, Mads Ruud
    {
        #region Fields
        private string _txt;
        private MyController _myController;
        #endregion

        public UCOperator(MyController myController)
        {
            InitializeComponent();
            _myController = myController;
            DataGridOperator();
        }

        public void DataGridOperator() //Tien - 13. maj, 24. maj
        {
            string[,] dgBinding = { {"Initials", "Initialer"}, {"IsEnabled", "Aktiv"} };
            DataGridTextColumn dgc;
            for (int i = 0; i < dgBinding.Length / 2; i++)
            {
                dgc = new DataGridTextColumn();
                dgc.Binding = new Binding(dgBinding[i, 0]);
                dgc.Header = dgBinding[i, 1];
                dgc.Width = 150;
                dgOperator.Columns.Add(dgc);
            }
            dgOperator.DataContext = _myController.GetOperator();
        }

        #region Events
        private void btnOperatorAdd_Click(object sender, RoutedEventArgs e) //Tien, Trang - 13. maj, 15. maj
        {
            string initials = txtInitialsAdd.Text;
            string pass = passAdd.Password;
            bool operatorExists = false;
            foreach (IOperator o in _myController.GetOperator())
            {
                if (o.Initials == initials)
                {
                    operatorExists = true;
                    break;
                }
            }
            if (operatorExists == false)
            {
                if (cbActiveAdd.SelectedIndex == 0)
                    _myController.AddOperator2Db(initials, pass, true);
                else
                    _myController.AddOperator2Db(initials, pass, false);
                dgOperator.DataContext = null;
                dgOperator.DataContext = _myController.GetOperator();
            }
            else
                MessageBox.Show("Operatør findes allerede i system");
        }
        private void btnOperatorEdit_Click(object sender, RoutedEventArgs e)
        {
            string initials = txtInitialsEdit.Text;
            string pass = passEdit.Password;
            string pass2 = passEdit2.Password;
            if (pass == pass2)
            {
                if (pass.Length == 0)
                    pass = string.Empty;
                if (cbActiveEdit.SelectedIndex == 0)
                    _myController.UpdateOperator(initials, pass, true);
                else
                    _myController.UpdateOperator(initials, pass, false);
                dgOperator.DataContext = null;
                dgOperator.DataContext = _myController.GetOperator();
            }
        }
        private void dgOperator_SelectionChanged(object sender, SelectionChangedEventArgs e) //Tien - 13. maj
        {
            if (dgOperator.Items.Count > 0)
            {
                IOperator o = (IOperator)dgOperator.SelectedItem;
                txtInitialsEdit.Text = o.Initials;
                _myController.RefreshOperatorSession();
                IOperatorSession ios = _myController.FindOperatorSession(o.Initials);
                lblStart.Content = "Start";
                lblEnd.Content = "Slut";
                if (ios != null)
                {
                    lblStart.Content = ios.StartTime.ToString();
                    lblEnd.Content = ios.EndTime.ToString();
                }
            }
        }
        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox tempTxt = (TextBox)e.Source;
            _txt = tempTxt.Text;
            if (tempTxt.Text == "Søg")
                tempTxt.Clear();
        }
        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox tempTxt = (TextBox)e.Source;
            if (tempTxt.Text == string.Empty || tempTxt.Text.Length == 0)
                tempTxt.Text = _txt;
        }
        private void txtInitialsAdd_TextChanged(object sender, TextChangedEventArgs e) //Tien - 13. maj
        {
            string initials = txtInitialsAdd.Text;
            if (!_myController.StrLen(initials, 3))
                MessageBox.Show("Initialer skal være på 3 tegn");
        }
        #endregion
    }
}
